package a66weding.com.jiehuntong.pager;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import org.json.JSONException;
import org.xutils.http.RequestParams;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.adapter.MyAdapter;
import a66weding.com.jiehuntong.application.DataWareHourse;
import a66weding.com.jiehuntong.beans.CommonResultBean;
import a66weding.com.jiehuntong.beans.DiyaddBean;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.GuestsseatBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.beans.User;
import a66weding.com.jiehuntong.common.utils.CUtils;
import a66weding.com.jiehuntong.common.utils.uploadimage.UpLoadUtil;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.http.UrlDealUtil;

/**
 * Created by admin on 2016/6/14.
 */
public class Diy_AddliuchengPager extends BasePager implements View.OnClickListener, AdapterView.OnItemClickListener, OnNetResultListener {

    private ListView lv;
    private MyAdapter mAdapter;
    private ArrayList<DiyaddBean> list;
    // 记录选中的条目数量
    private int checkNum;
    private Button btn_add;
    private RelativeLayout rl_title;
    private View myDiyView;
    private ImageView back_iv;
    private String mobileNum;
    private ImageView iv_all;
    private Set<String> data;

    public Diy_AddliuchengPager(Activity activity) {

        super(activity);
    }

    @Override
    protected View getView() {
        myDiyView = View.inflate(mActivity, R.layout.activity_diy_listview_add, null);
        rl_title = (RelativeLayout) myDiyView.findViewById(R.id.diy_title_rl);
        back_iv = (ImageView) myDiyView.findViewById(R.id.headleft_title_iv);
        iv_all = (ImageView) myDiyView.findViewById(R.id.item_add_iv);
        btn_add = (Button) myDiyView.findViewById(R.id.add_btn);
        lv = (ListView) myDiyView.findViewById(R.id.lv);
        list = new ArrayList<DiyaddBean>();
        // 为Adapter准备数据
        initDate();
        // 实例化自定义的MyAdapter
        mAdapter = new MyAdapter(list, mActivity);
        // 绑定Adapter
        lv.setAdapter(mAdapter);
        // 全选按钮的回调接口
        lv.setOnItemClickListener(this);
        back_iv.setOnClickListener(this);
        iv_all.setOnClickListener(this);
        rl_title.setOnClickListener(this);
        btn_add.setOnClickListener(this);
        return myDiyView;
    }
    private String[] strName  ;
    // 初始化数据
    private void initDate() {

        strName = new String[] {"订婚", "登记", "对戒", "酒店", "婚纱照", "婚庆", "礼服", "邀请", "婚品", "化妆司仪", "摄影录像", "婚车", "典礼", "回娘家", "蜜月"};
        list = new ArrayList<DiyaddBean>();
        for (int i = 0; i < strName.length; i++) {
            DiyaddBean object = new DiyaddBean();
            object.Name = strName[i];
            list.add(object);
        }
        data = new HashSet<>();
    }
    private boolean flag = true ;
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.item_add_iv:
                if(checkNum==15){
                    flag = false;
                }else{
                    flag = true;
                }
                if(flag){
                    iv_all.setBackgroundResource(R.mipmap.selete);
                    flag = false;
                    // 遍历list的长度，将MyAdapter中的map值全部设为true
                    for (int i = 0; i < list.size(); i++) {
                        MyAdapter.getIsSelected().put(i, true);
                        data.add(strName[i]);
                    }
                    // 数量设为list的长度
                    checkNum = list.size();
                    // 刷新listview和TextView的显示
                }else{
                    iv_all.setBackgroundResource(R.mipmap.no_selete);
                    flag = true;
                    for (int i = 0; i < list.size(); i++) {
                        MyAdapter.getIsSelected().put(i, false);
                        data.clear();
                    }
                    checkNum = 0;
                }
                dataChanged();
                break;
            case R.id.diy_title_rl:
                //提交

                mActivity.finish();
                break;
            case R.id.add_btn:
                if (checkNum > 0) {
                     getNetData();
                    mActivity.finish();
                } else {
                    CUtils.showToast("请选择您要新增的内容");
                }
                break;
            case R.id.headleft_title_iv:
                mActivity.finish();
                break;
        }
    }

    // 刷新listview和TextView的显示
    private void dataChanged() {
        // 通知listView刷新
        bianli(data);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // 取得ViewHolder对象，这样就省去了通过层层的findViewById去实例化我们需要的cb实例的步骤
        MyAdapter.ViewHolder holder = (MyAdapter.ViewHolder) view.getTag();
        // 改变CheckBox的状态
        holder.cb.toggle();
        // 将CheckBox的选中状况记录下来
        MyAdapter.getIsSelected().put(position, holder.cb.isChecked());
        // 调整选定条目
        if (holder.cb.isChecked() == true) {
            checkNum++;
            data.add(strName[position]);
        } else {
            checkNum--;
            data.remove(strName[position]);
        }
        if (checkNum == 15) {
            iv_all.setBackgroundResource(R.mipmap.selete);
        } else if (checkNum < 15) {
            iv_all.setBackgroundResource(R.mipmap.no_selete);
        }
        bianli(data );
    }

    private List<String> str;
    private String result ;
    public void bianli(Set<String> data){

        Iterator it=data.iterator();
        str = new ArrayList<>();
        if(data.size()==0){
            result = null;
        }else {
            while (it.hasNext()) {
                String o = (String) it.next();
                str.add(o);
                result = changeStr(str);
            }
        }
    }

    public String  changeStr(List<String> list){
        StringBuffer sb = new StringBuffer();
        for (int i = 0 ;i<list.size();i++){
            if(i ==0){
                sb.append(list.get(i));
            }else{
                sb.append(","+list.get(i));
            }
        }
        return sb.toString();
    }

    private CommonResultBean bean;
    @Override
    public void onSucess(SuccessBean sucessbean)  {
        Object data = sucessbean.getData();
        if(data instanceof CommonResultBean){
            bean = (CommonResultBean) data;
            switch (bean.getResCode()){
                case "0000":
                    CUtils.showToast("增加成功了");
                    break;
            }
        }

    }

    @Override
    public void onError(ErrorBean errorBean) {

    }
    //提交流程;
    private void getNetData(){
       String url = UrlDealUtil.getRealUrl(RequestCode.ADD_PROCESS);
        HashMap<String,String> paramsMap=new HashMap<>();
        paramsMap.put("BookTitle",result);
        HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, url, this, CommonResultBean.class, RequestCode.ADD_PROCESS, paramsMap);
        HttpHandleUtil.executeCommonPost(mActivity,httpRequestBean);
    }
}
